<?php

class Profile extends Controller {
	
	function index() {
		$this->set('title','Profile');
		$idUserOwner = $this->session->get('idUserActive');
		$this->session->set('idUserPassive',$idUserOwner);
		$CONTENT = $this->ProfileModel->GetWallPage($idUserOwner);
		$this->set('wall',$CONTENT[0]);
		$this->set('comment',$CONTENT[1]);
		$this->set('following',$CONTENT[2]);
		$this->set('follower',$CONTENT[3]);
		$this->set('profileinfo',$CONTENT[4]);
		$this->set('followingprofpict',$CONTENT[6]);
		$this->set('followerprofpict',$CONTENT[7]);
		$this->set('statusownerwall',$CONTENT[8]);
		$this->set('statusownerwallcomment', $CONTENT[9]);
	}
	function editprofile(){
		$idUser = $this->session->get('idUserActive');
		$CONTENT = $this->ProfileModel->GetEditProfilePage($idUser);
		$this->set('profileinfo', $CONTENT[0]);
		$this->set('follower', $CONTENT[1]);
		$this->set('following', $CONTENT[2]);
		$this->set('followingprofpict',$CONTENT[3]);
		$this->set('followerprofpict',$CONTENT[4]);
	}
	function wallpage($usernamePageOwner){
		$idusrowner = $this->ProfileModel->getIdUserbyUsername($usernamePageOwner);
		$idUserPageOwner = $idusrowner[0]['User']['idUser'];
		$this->session->set('idUserPassive',$idUserPageOwner);
		
		$idUserOwner = $this->session->get('idUserPassive');
		$idUserActive = $this->session->get('idUserActive');
		$statusfollower = $this->ProfileModel->GetStatusFollow($idUserOwner,$idUserActive);
		
		
		$CONTENT = $this->ProfileModel->GetWallPage($idUserOwner);
		
		$this->set('wall',$CONTENT[0]);
		$this->set('comment',$CONTENT[1]);
		$this->set('following',$CONTENT[2]);
		$this->set('follower',$CONTENT[3]);
		$this->set('profileinfo',$CONTENT[4]);
		$this->set('name_user',$CONTENT[5]);
		$this->set('followingprofpict',$CONTENT[6]);
		$this->set('followerprofpict',$CONTENT[7]);
		$this->set('statusfollower',$statusfollower);
		
		$statusownerwall = array();
		$statusownerwallcomment = array();
		foreach($CONTENT[0] as $iter){
			$idwall = $iter['Wall']['idWall'];
			$statusownerwall[$idwall] = $this->ProfileModel->GetStatusWallOwner($idUserActive, $idwall);
			foreach($CONTENT[1][$idwall] as $itercomment){
				$idcomment = $itercomment['Comment']['idComment'];
				$statusownerwallcomment[$idwall][$idcomment] = $this->ProfileModel->GetStatusCommentWallOwner($idUserActive, $idwall, $idcomment);
			}
		}
		
		$this->set('statusownerwallcomment', $statusownerwallcomment);
		$this->set('statusownerwall',$statusownerwall);
		if($this->session->get('idUserPassive') == $this->session->get('idUserActive')){
			$this->redirect(BASE_PATH.'/profile/index');
		}
	}
	function wallpost(){
		$field = array();
		$field['idUserSender'] = $this->session->get('idUserActive');
		$field['idUserOwner'] = $this->session->get('idUserPassive');
		$field['Content'] = $_POST['wallpost'];
		$field['Date'] = date('Y-m-d');
		$this->ProfileModel->AddWall($field);
		if($this->session->get('idUserPassive') == $this->session->get('idUserActive')){
			$this->redirect(BASE_PATH.'/profile/index');
		}else{
			$iduser = $this->session->get('idUserPassive');
			$usernameArr= $this->ProfileModel->getUsernamebyIdUser($iduser);
			$username = $usernameArr[0]['User']['Username'];
			$this->redirect(BASE_PATH.'/profile/wallpage/'.$username);
		}
		
	}
	function deletewall($idWall){
		$this->ProfileModel->DeleteWall($idWall);
		$this->ProfileModel->DeleteCommentWall($idWall);
		if($this->session->get('idUserActive')==$this->session->get('idUserPassive')){
			$this->redirect(BASE_PATH.'/profile/index');
		}else{
			$iduser = $this->session->get('idUserPassive');
			$usernameArr= $this->ProfileModel->getUsernamebyIdUser($iduser);
			$username = $usernameArr[0]['User']['Username'];
			$this->redirect(BASE_PATH.'/profile/wallpage/'.$username);
		}
	}
	function deletecomment($idComment){
		$this->ProfileModel->DeleteCommentWallbyidComment($idComment);
		if($this->session->get('idUserActive')==$this->session->get('idUserPassive')){
			$this->redirect(BASE_PATH.'/profile/index');
		}else{
			$iduser = $this->session->get('idUserPassive');
			$usernameArr= $this->ProfileModel->getUsernamebyIdUser($iduser);
			$username = $usernameArr[0]['User']['Username'];
			$this->redirect(BASE_PATH.'/profile/wallpage/'.$username);
		}
	}
	function profilephoto(){
		$idUserOwner = $this->session->get('idUserActive');
		$this->session->set('idUserPassive',$idUserOwner);
		$this->redirect(BASE_PATH.'/profile/thumbnailphoto');
	}
	function searchphoto(){
		$namesearch = $_POST['photosearchbox'];
		$this->set('title','Profile --Thumbnail Photo');
		$idUserOwner = 0;
		$useractive = $this->session->get('idUserActive');
		$userpassive = $this->session->get('idUserPassive');
		if($useractive==$userpassive){
			// Melihat daftar photo sendiri
			$idUserOwner = $useractive;
			$CONTENT = $this->ProfileModel->GetSearchPhotoPage($idUserOwner,1, $namesearch);
		}else{
			// Melihat daftar photo orang lain
			$idUserOwner = $userpassive;
			$statusfollower = $this->ProfileModel->GetStatusFollow($userpassive, $useractive);
			if($statusfollower==1){
				// 1. Jika dia follower
				$CONTENT = $this->ProfileModel->GetSearchPhotoPage($idUserOwner,2, $namesearch);
			}else{
				// 2. Jika dia bukan follower
				$CONTENT = $this->ProfileModel->GetSearchPhotoPage($idUserOwner,3, $namesearch);
			}			
		}
		$this->set('profileinfo',$CONTENT[0]);
		$this->set('thumbnailpicture',$CONTENT[1]);
		$this->set('name_user',$CONTENT[2]);
	}
	function thumbnailphoto(){
		$this->set('title','Profile --Thumbnail Photo');
		$idUserOwner = 0;
		$useractive = $this->session->get('idUserActive');
		$userpassive = $this->session->get('idUserPassive');
		if($useractive==$userpassive){
			// Melihat daftar photo sendiri
			$idUserOwner = $useractive;
			$CONTENT = $this->ProfileModel->GetPhotoPage($idUserOwner,1);
		}else{
			// Melihat daftar photo orang lain
			$idUserOwner = $userpassive;
			$statusfollower = $this->ProfileModel->GetStatusFollow($userpassive, $useractive);
			if($statusfollower==1){
				// 1. Jika dia follower
				$CONTENT = $this->ProfileModel->GetPhotoPage($idUserOwner,2);
			}else{
				// 2. Jika dia bukan follower
				$CONTENT = $this->ProfileModel->GetPhotoPage($idUserOwner,3);
			}			
		}
		
		$this->set('profileinfo',$CONTENT[0]);
		$this->set('thumbnailpicture',$CONTENT[1]);
		$this->set('name_user',$CONTENT[2]);
	}
	
	function commentwallpost($idWall){
		$field = array();
		$field['idUserSender'] = $this->session->get('idUserActive');
		$field['idWall'] = $idWall;
		
		$field['Type'] = 1;
		$field['Content'] = $_POST['commentwall'];
		$field['Date'] = date('Y-m-d');
		
		$this->ProfileModel->AddCommentWall($field);
		$idUserOwner = $this->ProfileModel->getIdUserOwnerbyIdWall($idWall);
		if($this->session->get('idUserPassive') == $this->session->get('idUserActive')){
			$this->redirect(BASE_PATH.'/profile/index');
		}else{
			$iduser = $this->session->get('idUserPassive');
			$usernameArr= $this->ProfileModel->getUsernamebyIdUser($iduser);
			$username = $usernameArr[0]['User']['Username'];
			$this->redirect(BASE_PATH.'/profile/wallpage/'.$username);
		}
	}
	function makeprofilepicture($idPhoto){
		$idUser = $this->session->get('idUserActive');
		$this->ProfileModel->UpdateProfilePicture($idUser, $idPhoto);
		$this->redirect(BASE_PATH.'/profile/index');
	}
	function commentphoto($idPhoto){
		$field = array();
		$field['idUserSender'] = $this->session->get('idUserActive');
		$field['idPhoto'] = $idPhoto;
		
		$field['Type'] = 0;
		$field['Content'] = $_POST['commentphoto'];
		$field['Date'] = date('Y-m-d');
		
		$this->ProfileModel->AddCommentPhoto($field);
		
		$this->redirect(BASE_PATH.'/profile/viewphoto/'.$idPhoto);
		
	}
	function search(){
		$iduser = $this->session->get('idUserActive');
		$name = $_POST['searchbox'];
		$CONTENT = $this->ProfileModel->GetSearchResultPage($iduser, $name);
		$this->set('profileinfo',$CONTENT[0]);
		$this->set('search',$CONTENT[1]);
		$this->set('following',$CONTENT[2]);
		$this->set('follower',$CONTENT[3]);
		
		$this->set('followingprofpict',$CONTENT[4]);
		$this->set('followerprofpict',$CONTENT[5]);
		$statusfollow = array();
		$iduserfollowing = 0;
		foreach($CONTENT[1] as $itersearch){
			$iduserfollowing = $itersearch['User']['idUser'];
			$statusfollow[$iduserfollowing] = $this->ProfileModel->GetStatusFollow($iduserfollowing, $iduser);
		}
		$this->set('statusfollow',$statusfollow);
	}
	function follow($iduserfollowing){
		$iduser = $this->session->get('idUserActive');
		$this->ProfileModel->Follow($iduserfollowing,$iduser);
		$usernameArr= $this->ProfileModel->getUsernamebyIdUser($iduser);
		$username = $usernameArr[0]['User']['Username'];
		$this->redirect(BASE_PATH.'/profile/wallpage/'.$username);
	}
	function unfollow($iduserfollowing){
		$iduser = $this->session->get('idUserActive');
		$this->ProfileModel->Unfollow($iduserfollowing,$iduser);
		$this->redirect(BASE_PATH.'/profile/index');
	}
	function insertphoto(){
		$iduser = $this->session->get('idUserActive');
		$username = $this->ProfileModel->getUsernamebyIdUser($iduser);
		$field = array();
		$field['iduserown'] = $iduser;
		$field['idcategory'] = 0;
		
		$field['username'] = $username[0]['User']['Username'];
		$field['description'] = $_POST['description'];
		$field['keyword'] = $_POST['keyword'];
		$field['date'] = $_POST['date'];
		$field['datenow'] = date('Y-m-d');
		$field['viewsetting'] = $_POST['viewsetting'];
		$field['commentsetting'] = $_POST['commentsetting'];
		$field['filephoto'] = $_FILES['foto'];
		$result = $this->ProfileModel->AddPhoto($field);
		if($result==0){
			// kalo gagal
			$this->redirect(BASE_PATH."/profile/upladphoto");
		}else if($result==1){
			// kalo berhasil
			$this->redirect(BASE_PATH."/profile/thumbnailphoto");
		}
	}
	function viewphoto($idPhoto){
		$this->set('title','Profile --Thumbnail Photo');
		$idUserOwner = $this->session->get('idUserActive');
		
		$CONTENT = $this->ProfileModel->GetViewPhotoPage($idUserOwner, $idPhoto);
		$this->set('profileinfo',$CONTENT[0]);
		$this->set('pictureinfo',$CONTENT[1]);
		$this->set('comment',$CONTENT[2]);
		$this->set('statusphotoowner',$CONTENT[3]);
		$this->set('following',$CONTENT[4]);
		$this->set('followingusername',$CONTENT[5]);
		$this->set('usernamehastagged',$CONTENT[6]);
	}
	function photovalue($idPhoto){
		$value = $_POST['valuephoto'];
		$tempvalue = $this->ProfileModel->getPhotoValuebyIdPhoto($idPhoto);
		$lastvalue = $tempvalue[0]['Photo']['TotalValue'];
		$realvalue = $lastvalue + $value;
		
		$idUserSender = $this->session->get('idUserActive');
		$this->ProfileModel->AddPhotoValue($idPhoto,$idUserSender,$value);
		$this->ProfileModel->SetPhotoValue($idPhoto, $realvalue);
		
		$this->redirect(BASE_PATH.'/profile/viewphoto/'.$idPhoto);
	}
	function tagphoto($idPhoto){
		$idUserTagged = $_POST['usertagged'];
		$this->ProfileModel->AddPhotoTag($idPhoto, $idUserTagged);
		$this->redirect(BASE_PATH.'/profile/viewphoto/'.$idPhoto);
	}
	function editphoto($idPhoto){
		$iduser = $this->session->get('idUserActive');
		$CONTENT = $this->ProfileModel->GetEditPhotoPage($iduser, $idPhoto);
		$this->set('profileinfo', $CONTENT[0]);
		$this->set('pictureinfo', $CONTENT[1]);
	}
	function logout(){
		$this->session->destroy();
		$this->redirect(BASE_PATH.'/home/index');
	}
	function beforeAction(){
		$this->set('title',null);
	}
	function uploadphoto(){
		$iduser = $this->session->get('idUserActive');
		$CONTENT = $this->ProfileModel->GetUploadPhotoPage($iduser);
		$this->set('profileinfo',$CONTENT[0]);
	}
	function submiteditprofile(){
		$field = array();
		$field['Name'] = $_POST['fname'];
		$field['Password'] = $_POST['fpass'];
		$field['Date'] = $_POST['ftanggal'];
		$field['Email'] = $_POST['femail'];
		$field['Gender'] = $_POST['fgender'];
		$field['Location'] = $_POST['flokasi'];
		$field['WallSetting'] = $_POST['fwallsetting'];
		$field['idUser'] = $this->session->get('idUserActive');
		$result = $this->ProfileModel->SetProfileInfo($field);
		$this->redirect(BASE_PATH.'/profile/index');
	}
	function submiteditphoto($idPhoto){
		$field = array();
		$field['FileName'] = $_POST['filename'];
		$field['Description'] = $_POST['description'];
		$field['KeyWord'] = $_POST['keyword'];
		$field['Date'] = $_POST['date'];
		$field['ViewSetting'] = $_POST['viewsetting'];
		$field['CommentSetting'] = $_POST['commentsetting'];
		$field['idPhoto'] = $idPhoto;
		$this->ProfileModel->setInfoPicture($field);
		$this->redirect(BASE_PATH.'/profile/viewphoto/'.$idPhoto);
	}
	function deletephoto($idPhoto){
		$this->ProfileModel->DeletePhoto($idPhoto);
		$this->ProfileModel->DeleteCommentWall($idPhoto);
		$this->redirect(BASE_PATH.'/profile/thumbnailphoto');
	}
	function admin(){
		$this->session->set('idUserActive',1);
		$user = $this->ProfileModel->getUserSetting();
		$this->set('user', $user);
	}
	function activateuser($iduser){
		$user = $this->ProfileModel->setStatusAllow($iduser, 0);
		$this->redirect(BASE_PATH.'/profile/admin');
	}
	function blockuser($iduser){
		$user = $this->ProfileModel->setStatusAllow($iduser, 1);
		$this->redirect(BASE_PATH.'/profile/admin');
	}
	function deleteuser($iduser){
		$user = $this->ProfileModel->setStatusAllow($iduser, 2);
		$this->redirect(BASE_PATH.'/profile/admin');
	}
	function message(){
            $this->set('title','Message');
            $idUserOwner = $this->session->get('idUserActive');
            $this->session->set('idUserPassive',$idUserOwner);
            $CONTENT = $this->ProfileModel->GetWallPage($idUserOwner);
            $this->set('wall',$CONTENT[0]);
            $this->set('comment',$CONTENT[1]);
            $this->set('following',$CONTENT[2]);
            $this->set('follower',$CONTENT[3]);
            $this->set('profileinfo',$CONTENT[4]);
            $this->set('followingprofpict',$CONTENT[6]);
            $this->set('followerprofpict',$CONTENT[7]);
            $this->set('statusownerwall',$CONTENT[8]);
            $this->set('statusownerwallcomment', $CONTENT[9]);
                        
            $this->set('message',  $this->ProfileModel->getMessage($this->session->get('idUserActive')));
    }
        function newmessage(){
            global $session;
            $field = array ();
            $field['subject'] = $_POST['msubject'];
            $field['content'] = $_POST['mcontent'];
            $field['receiver'] = explode(',',$_POST['mto']);
            $field['senderId'] = $this->session->get('idUserActive');
            
            $this->ProfileModel->insertConversation($field);

            $this->redirect(BASE_PATH.'/profile/message');
        }
        function openmessage($idconv){
            $result = $this->ProfileModel->getThisMessage($idconv);
            $subject = $result[0]['subject'];
            $this->set('title',$subject);
            $this->set('subject',$subject);
            $this->set('message',$result);
            $this->set('idconv',$idconv);
            
            
            $idUserOwner = $this->session->get('idUserActive');
            $this->session->set('idUserPassive',$idUserOwner);
            $CONTENT = $this->ProfileModel->GetWallPage($idUserOwner);
            $this->set('wall',$CONTENT[0]);
            $this->set('comment',$CONTENT[1]);
            $this->set('following',$CONTENT[2]);
            $this->set('follower',$CONTENT[3]);
            $this->set('profileinfo',$CONTENT[4]);
            $this->set('followingprofpict',$CONTENT[6]);
            $this->set('followerprofpict',$CONTENT[7]);
            $this->set('statusownerwall',$CONTENT[8]);
            $this->set('statusownerwallcomment', $CONTENT[9]);
        }

        function reply($idconv){
            $content = $_POST['mreply'];
            $idSender = $this->session->get('idUserActive');
            $this->ProfileModel->insertReply($idconv,$idSender,$content);
            $this->redirect(BASE_PATH.'/profile/openmessage/'.$idconv);
        }
}
